//
//  SelectExamTimeSignUpmainCell.h
//  CommunityNannyO2O
//
//  Created by apple on 15/8/13.
//  Copyright (c) 2015年 Apple. All rights reserved.
//

#import "SelectExamTimeSignUpmainCell.h"
#import "APIAdress.h"
#import "MBProgressHUD+YC.h"
#import "MJExtension.h"
#import "AccountTool.h"
#import "UIViewController+Create.h"
#import "CustomTextField.h"
#import "AppConfig.h"
#import "QXNetWorkTool.h"
#import "SelectExamTimeSignUpCell.h"
#import "SelectExamTiemModel.h"

@interface SelectExamTimeSignUpmainCell()<QXNetWorkToolDelegate,
                        UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (weak, nonatomic) IBOutlet UIButton *confirm;

@property (copy, nonatomic) NSString *selectStartHour;
@property (copy, nonatomic) NSString *selectStartMinute;
@property (copy, nonatomic) NSString *selectEndHour;
@property (copy, nonatomic) NSString *selectEndMinute;

@end

@implementation SelectExamTimeSignUpmainCell

@synthesize timeData;

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"SelectExamTimeSignUpmainCell";
    SelectExamTimeSignUpmainCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle] loadNibNamed:ID owner:nil options:nil]firstObject];
        
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    
    return cell;
}

-(void)awakeFromNib
{
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.confirm.layer.cornerRadius = 5;
    self.confirm.layer.masksToBounds = YES;
}


//tableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.timeData count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 71;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SelectExamTimeSignUpCell *cell = [SelectExamTimeSignUpCell cellWithTableView:tableView];

    SelectExamTiemModel *model = [self.timeData objectAtIndex:indexPath.row];
    NSString *timeStr = model.exam_time;
    
    NSString *hour = [timeStr substringWithRange:NSMakeRange(0, 2)];
    NSString *minute = [timeStr substringWithRange:NSMakeRange(3, 2)];
    
    //每个时间段
    NSMutableArray *eachTime = [self getTimeByStartHour:[hour intValue]
                                                StartMinute:[minute intValue]
                                                  Interval:20];
    
    [cell setTimeStartHour:[eachTime objectAtIndex:0]
               StartMinute:[eachTime objectAtIndex:1]
                   EndHour:[eachTime objectAtIndex:2]
                 EndMinute:[eachTime objectAtIndex:3]];
    
    NSString *tmpShour = cell.startHour;
    NSString *tmpSminute = cell.startMinute;
    NSString *tmpEhour = cell.endHour;
    NSString *tmpEminute = cell.endMinute;
    
    if ([tmpShour intValue] < 10)
    {
        tmpShour = [NSString stringWithFormat:@"0%@", tmpShour];
    }
    
    if ([tmpSminute intValue] < 10)
    {
        tmpSminute = [NSString stringWithFormat:@"0%@", tmpSminute];
    }
    
    if ([tmpEhour intValue] < 10)
    {
        tmpEhour = [NSString stringWithFormat:@"0%@", tmpEhour];
    }
    
    if ([tmpEminute intValue] < 10)
    {
        tmpEminute = [NSString stringWithFormat:@"0%@", tmpEminute];
    }

    cell.time.text = [NSString stringWithFormat:@"%@:%@ ~ %@:%@", tmpShour,
                      tmpSminute, tmpEhour, tmpEminute];
    
    cell.timeId = model.id;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *array = [tableView visibleCells];
    
    for (SelectExamTimeSignUpCell *cell in array)
    {
        cell.check.hidden = YES;
    }
    
    SelectExamTimeSignUpCell *cell = (SelectExamTimeSignUpCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    self.selectStartHour = cell.startHour;
    self.selectStartMinute = cell.startMinute;
    self.selectEndHour = cell.endHour;
    self.selectEndMinute = cell.endMinute;
    
    cell.check.hidden = NO;
}

-(IBAction)finish:(id)sender
{
    if ([self.finishDelegate respondsToSelector:
            @selector(finishSHour:SMinute:EHour:EMinute:)])
    {
        [self.finishDelegate finishSHour:self.selectStartHour
                                 SMinute:self.selectStartMinute
                                   EHour:self.selectEndHour
                                 EMinute:self.selectEndMinute];
    }
}

- (NSMutableArray *)getTimeByStartHour:(int)startHour
                                StartMinute:(int)startMinute
                                   Interval:(int)interval
{
    int curHo = startHour;
    int curM = startMinute;
    
    int sH = curHo;
    int sM = curM;
    
    curM += interval;
    if (curM >= 60)
    {
        ++curHo;
        curM = curM - 60;
    }

    NSMutableArray * timeAry = [[NSMutableArray alloc] init];
    
    [timeAry addObject:[NSString stringWithFormat:@"%d", sH]];
    [timeAry addObject:[NSString stringWithFormat:@"%d", sM]];
    [timeAry addObject:[NSString stringWithFormat:@"%d", curHo]];
    [timeAry addObject:[NSString stringWithFormat:@"%d", curM]];
    
    return timeAry;
}

//获得时间数组，现在不用了
- (NSMutableArray *)getTimeArrayByStartHour:(int)startHour
                 StartMinute:(int)startMinute
                 EndHour:(int)endHour
                 EndMinute:(int)endMinute
                 Interval:(int)interval
{
    NSMutableArray * timeAry = [[NSMutableArray alloc] init];

    int curHo = startHour;
    int curM = startMinute;
    
    for (;;)
    {
        int sH = curHo;
        int sM = curM;
        
        curM += interval;
        if (curM >= 60)
        {
            ++curHo;
            curM = 0;
        }
        
        if (curHo < endHour)
        {
            [timeAry addObject:[NSArray arrayWithObjects:
                                [NSString stringWithFormat:@"%d", sH],
                                [NSString stringWithFormat:@"%d", sM],
                                [NSString stringWithFormat:@"%d", curHo],
                                [NSString stringWithFormat:@"%d", curM],
                                nil]];
        }
        else
        {
             if (curM <= endMinute)
             {
                 [timeAry addObject:[NSArray arrayWithObjects:
                                     [NSString stringWithFormat:@"%d", sH],
                                     [NSString stringWithFormat:@"%d", sM],
                                     [NSString stringWithFormat:@"%d", curHo],
                                     [NSString stringWithFormat:@"%d", curM],
                                     nil]];
             }
             else
                 break;
        }
    }
    
    return timeAry;
}

@end




















